package
{
    import away3d.containers.ObjectContainer3D;
    import away3d.containers.View3D;
    import away3d.debug.Debug;
    import away3d.lights.PointLight;
    import away3d.materials.BitmapMaterial;
    import away3d.materials.ColorMaterial;
    import away3d.materials.methods.OutlineMethod;
    import away3d.primitives.Cube;
    
    import flash.display3D.Context3D;
    
    Context3D;
    
    import away3d.primitives.Sphere;
    
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import away3d.primitives.Plane;
    import camera.EnvironmentCameraController;
    import environment.Environment;
    import objects.ColorPlane;
    import camera.HoverDragController;
    import flash.display.BitmapData;
    import flash.display.Bitmap;
    import objects.BitmapPlane;
    import flash.events.MouseEvent;
    import flash.geom.Vector3D;
    import microbes.Microbe;
    import objects.Object3DUtil;
    import game.GameTimer;
    
    [SWF(frameRate="60", width="1024", height="600")]
    public class Microbe3D extends Sprite
    {
        var plane :BitmapPlane;
        
        private var _view : View3D;
        
        private var _ctr : ObjectContainer3D;
        private var _light : PointLight;
        private var _light2 : PointLight;
        
        private var _camController : EnvironmentCameraController;
        
        [Embed(source="../embeds/crossnrm.jpg")]
        private var Image : Class;
        
        private var cursorObject : Sphere;
        
        protected var allMicrobes : Array = [];
        
        protected var env : Environment;
        
        public function Microbe3D()
        {
            super();
            
            addEventListener(Event.ENTER_FRAME, enterFrameHandler);
            addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
            addEventListener(MouseEvent.CLICK, clickHandler);
            
            
            _view = new View3D();
            
            
            
            this.addChild(_view);
            
            var cm : ColorMaterial = new ColorMaterial(0x00FF00, 1);
            
            cursorObject = new Sphere(cm, 2, 16,12, true);
            
            _ctr = new ObjectContainer3D();
            _view.scene.addChild(_ctr);
            _view.backgroundColor = 0xFFFFFF;
            env = new Environment(_ctr, 64, 64);
            
            env.generateEnvironment(10, 10, 10);
            
            _camController = new EnvironmentCameraController(_view.camera, stage, env);
            
            _ctr.addChild(cursorObject);
            
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            stage.addEventListener(Event.RESIZE, onStageResize);
        }
        
        private function mouseMoveHandler(event :MouseEvent) : void
        {
            var w : Vector3D = getMouseCoordinate(event.stageX, event.stageY);
            
            if (w)
            {
                cursorObject.x = w.x;
                cursorObject.y = w.y;
                cursorObject.z = w.z;
            }
        }
        
        private function onStageResize(event : Event) : void
        {
            _view.width = stage.stageWidth;
            _view.height = stage.stageHeight;
        }
        
        private function enterFrameHandler(ev : Event) : void
        {
            var elapsed :Number = GameTimer.update();
            
            _view.render();
            
            for each (var microbe :Microbe in allMicrobes)
            {
                microbe.update(elapsed, env);
            }
           
            env.nutrientLayer.envPlane.invalidateVertexBuffer();
        }
        
        protected function getMouseCoordinate(mouseX : Number, mouseY : Number) : Vector3D
        {
            var v : Vector3D = _view.unproject(mouseX, mouseY);
            
            var cp : Vector3D = new Vector3D(_view.camera.x, _view.camera.y, _view.camera.z);
            
            var end : Vector3D = v.subtract(cp);
            end.normalize();
            end.scaleBy(10000);
            
            end = cp.add(end);
            
            var final : Vector3D = Object3DUtil.lineSegIntersectPlane(cp, end, new Vector3D(0,0,0), new Vector3D(0,0,1));
            
            return final;
        }
        
        
        protected function clickHandler(event :MouseEvent) : void
        {
            var microbe : Microbe = new Microbe(env);
            var w : Vector3D = getMouseCoordinate(event.stageX, event.stageY);
            
            _ctr.addChild(microbe.object3D);
            
            microbe.x = w.x / Environment.SIZE_MULTIPLIER;
            microbe.y = w.y / Environment.SIZE_MULTIPLIER;
            
            allMicrobes.push(microbe);
        }
    }
}
